class Surveys::SurveysController < ApplicationController
  before_filter :auth
  layout "surveys/surveys" 

   protect_from_forgery :except => [:new_question , :add_question, :add_questions ,:create_question]
  def index
     @tests = Survey.find_all_by_professional(false)
  end

    
  # 最初的new  后面有new 是复杂倾向测试..
  # def new
  #end
  
  def  edit
    if(params[:id])
      @test = Survey.find(params[:id])
    else
      @test = Survey.new(:name => "测试名称", :description => "帮助文本")
      @test.save!
    end
  end
  
  def update
    @test = Survey.find(params[:test][:id])
    @test.update_attributes(params[:test])

    respond_to do |format|
      format.html { render :partial => "test"}
    end
  end
  
  def create_question
    @test = Survey.find(params[:test_id])
    @question = Question.new(params[:question])
    @question.survey = @test
    @question.save
    if params["choice.content"]
      i = 0;
      params["choice.content"].each do |content|
        choice = Choice.new(:content => content, :value => params["choice.value"][i])
        choice.question = @question
        choice.save 
        i += 1
      end
    end
    
    @heighlight_id = @question.id
    
    respond_to do |format|
      format.html { render :partial => "questions"}
    end
  end
  
  #鍒犻櫎
  def destroy
    @test = Survey.find(params[:id])
    @test.destroy
		redirect_to(:action => "index")
  end  
  
  #相应拖拽事件，排列quesiton的顺序
  def sort
    @test = Survey.find(params[:id])
    
    first = params['question-list'][0]
    question = Question.find(first)
    
    #如果有章节，第一个必须是章节，不能是题目
    if hasPart(@test) and question.marker != QUESTION_MARKER_PART 
      render :noting => true
    end
    
    @test.questions.each do |q|  
      q.position = params['question-list'].index(q.id.to_s) + 1  
      q.save!  
    end  

    classify

    render :noting => true  
  end   

  #把题目都放到章节中
  def classify
    hash = Hash.new
    part_q = nil
    params['question-list'].each do |id|
      q = Question.find(id)
      if(q.marker == QUESTION_MARKER_PART)
        part_q = q
      else
        q.part = part_q
        q.save!
      end
    end
  end
  
  #判断有没有章节
  def hasPart(test)
    test.questions.each do |q|
      if q.marker == QUESTION_MARKER_PART 
        return true
      end
    end
    return false
  end
  
  #测试完成，可以上线
  def finish 
    @test = Survey.find(params[:id])
    @test.status = TEST_STATUS_FINISHED
    @test.save
    
    respond_to do |format|
      format.html { redirect_to(:action => "index") }
      format.xml  { head :ok }
    end
  end
  
  #查询测试是否有章节标题
  def has_part
    respond_to do |format|
      if Survey.find_by_marker(QUESTION_MARKER_PART)
        format.html { redirect :text => "has part!" }
      else
        format.html { redirect :text => ""}
      end
    end
  end
  
  #给测试增加图片
  def add_image_fortest
   @test=Survey.find(params[:id])
  end 
  def create_image_fortest
  @image = Image.new(params[:image])
  @image.test_id=params[:t_id]
  @image.filename=Date.new.strftime('%Y-%m-%d %H:%M:%S').to_s+"_"+@image.filename
  if @image.save
  flash[:notice] = '图片添加成功'
  redirect_to(:action=>"index" )
  else
  redirect_to(:action=>"index" )
  end 
  end

# 修改复杂倾向测试
    #新建一个测试
  def new
    @test=Survey.new()
    @test.has_part="yes"
  end
 #===============  
   def new_test
    @test=Survey.new(params[:test])
    @test.t_type=TEST_TYPE_MANY_TREND
    @test.professional = false
     if @test.save!
        redirect_to :action=>"new_addtrend" , :id=>@test.id
     else
       render :action=>"new"
     end
   end
 #============== 
#    完成测试创建并新建其倾向
  def new_addtrend
    #@test=Survey.new(params[:test])
    #@test.t_type=TEST_TYPE_MANY_TREND
    # if @test.save
    @test=Survey.find_by_id(params[:id])
    @trend_scales = Array.new
    for i in  1..@test.trend_num
       trend= TrendScale.new
       trend.survey_id=@test.id
        @trend_scales << trend
    end
  end 
#========================
 def new_trend
    @test=Survey.find_by_id(params[:t_id])
    ids=@test.trend_num.to_i
    begin
      for i in 1..ids
        @trend=TrendScale.new(params["trend"+i.to_s])
        @trend.save!
      end
      redirect_to :action=>"new_addpart" , :id=>@test.id
    rescue Exception
      render :action=>"new_addtrend"
    end
 end
#========================
 #完成倾向创建并新建其 part 
  def new_addpart
    #@test=Survey.find_by_id(params[:t_id])
   # ids=@test.trend_num.to_i
   # begin
    #for i in 1..ids
     # @trend=TrendScale.new(params["trend"+i.to_s])
     # @trend.save!
   # end
   @test=Survey.find_by_id(params[:id])
 
    @parts=Array.new
    for i in 1..@test.part_num
      part=Part.new
      part.survey_id=@test.id
      @parts << part
   end
   # rescue Exception
    # render :action=>"new_addtrend"
    #end
  end
 
 #============================
  def new_part
    #首先保存上一步新建的 part 
    @test=Survey.find_by_id(params[:t_id])
    ids=@test.part_num.to_i
    begin
    for i in 1..ids
      @part=Part.new(params["part"+i.to_s])
      @part.save!
    end
      redirect_to :action=>"new_report" , :id=>@test.id
    rescue
      render :action=>"new_addpart" , :id=>params[:t_id]
    end
  end
 #============================
 # 完成part 创建并新建report
  def new_report
  #首先保存上一步新建的 part 
    @test=Survey.find_by_id(params[:id])
    ids=@test.part_num.to_i
    for i in 1..ids
    @part=Part.new(params["part"+i.to_s])
    @part.save
    end
    #新建report 
   @parts=@test.parts
   @trends=@test.trend_scales
  end
  #完成report  并开始添加题目
  def new_complete
    @test=Survey.find_by_id(params[:t_id])
   for p_num in 1..@test.part_num
    pa="part"+p_num.to_s
    for  trend_num in 1..@test.trend_num
      tr="trend"+trend_num.to_s
      report=Report.new(params[:report][pa][tr])
      report.value=0
      report.save
    end
   end
   @parts=@test.parts
  end
  
  def add_question
    @part=Part.find_by_id(params[:id])
    @test=Survey.find_by_id(@part.survey_id)
  end
  
  def new_question
    @part = Part.find(params[:part_id])
    @test = Survey.find(params[:t_id])
    @question = Question.new(params[:question])
    @question.part = @part
    @question.survey=@test
    @question.save
    if params["choice.content"]
        i = 0;
        params["choice.content"].each do |content|
          choice = Choice.new(:content => content, :value => params["choice.value"][i])
          choice.question = @question
          choice.save 
          i += 1
        end
    end
    respond_to do |format|
       format.html { render :partial => "questions"}
    end
  end
  
  def add_questions
    @part=Part.find_by_id(params[:id])
    @test=Survey.find_by_id(@part.survey_id)
  end

  def helps
  end
end
